<!--
 * @author guojiteng
 -->
<template>
  <div class="commodity-container">
    <el-collapse-transition>
      <div class="commodity-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search"/>
        </span>
        <el-input
            v-model="listQuery.goodsName"
            clearable
            placeholder="输入商品名称进行搜索"
            class="search-input"
            @keyup.enter.native="handleFilter"
        />
        <div class="commodity-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter"
          >搜索
          </el-button
          >
          <el-button class="filter-item" type="primary" @click="handleReset"
          >重置
          </el-button
          >
        </div>
      </div>
    </el-collapse-transition>
    <div class="commodity-btn">
      <div class="btn-title">
        <span>商品设置</span>
      </div>
      <div class="btn-btns">
        <el-button style="margin-left: 10px" @click="handleAddByBusiness"
        >按商家添加商品
        </el-button
        >
        <el-button style="margin-left: 10px" @click="handleCreate"
        >添加商品
        </el-button
        >
        <el-button style="margin-left: 10px" @click="handleDeletes"
        >批量删除
        </el-button
        >
        <el-button
            style="padding: 10px 30px"
            @click="handleCancel"
            type="primary"
        >返回
        </el-button
        >
      </div>
    </div>
    <div v-loading="listLoading" class="commodity-list">
      <el-row>
        <el-col v-for="(item, index) in commodityList" :key="item.id" :span="5">
          <div class="commodity-list-item">
            <div class="checkBox">
              <el-checkbox
                  v-model="item.checked"
                  @change="chooseCommFun($event, item.goodsId)"
              ></el-checkbox>
            </div>
            <div class="item-img">
              <img :src="item.goodsImage" class="bg"/>
            </div>
            <div class="item-title" :title="item.goodsName">
              {{ item.goodsName }}
            </div>
            <p class="item-mark etc">商品编号：{{ item.goodsId || "" }}</p>
            <p class="item-mark etc">
              售价：<span class="price">{{ "￥" + getDouble(item.salePrice) || "" }}</span>
            </p>
            <div class="item-btns">
              <!-- <el-button type="text" @click=""
                >上架</el-button
              > -->
              <el-button type="text" @click="beforeDel(item)">删除</el-button>
            </div>
          </div>
        </el-col>
        <div v-if="commodityList.length <= 0" class="nodata">
          <img src="../images/nodata.png"/>
        </div>
      </el-row>
    </div>
    <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
    />

    <choose-comm
        v-if="showChoose"
        @cancel="commCancel"
        :activityd="id"
    ></choose-comm>
    <business-list @cancel="showDetail = false" v-if="showDetail" :type="2" @chooseAll="chooseAll"></business-list>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import {queryPage, del, addGoodsByBusiness} from '../api/commodity'
import {mapGetters} from 'vuex'
import permission from '@/directive/permission/index.js' // 权限判断指令
import ChooseComm from './../components/commodity/index'
import BusinessList from '@/views/welfare/grant/components/businessList'

export default {
  name: 'Commodity',
  components: {Pagination, ChooseComm, BusinessList},
  directives: {permission},
  data() {
    return {
      showChoose: false,
      listLoading: false,
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        goodsName: ''
      },
      commodityList: [],
      view: '/API-DOCUMENT/document/file/download?id=',
      id: '',
      showbank: false,
      chooseCommList: [],
      isDel: false,
      showDetail: false,
    }
  },
  computed: {
    ...mapGetters([
      'sidebar'
    ])
  },
  created() {
    this.id = this.$route.query.id
    this.getList()
  },
  methods: {
    getDouble(p) {
      var re = /([0-9]+.[0-9]{2})[0-9]*/;
      return p ? p.replace(re, "$1") : ''

    },
    chooseCommFun(e, id) {

      let index = this.chooseCommList.indexOf(id)
      if (e) {
        if (index == -1) {
          this.chooseCommList.push(id)
        }
      } else {
        if (index != -1) {
          this.chooseCommList.splice(index, 1)
        }
      }
    },
    commCancel() {
      this.showChoose = false
      this.getList()
    },
    /**
     * 按商家添加商品
     */
    handleAddByBusiness() {
      this.showDetail = true
    },
    handleCreate() {
      this.showChoose = true
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCancel() {
      this.$router.push({
				path:this.$filterPath("index")
				, query: {t: this.$route.query.t}})
    },
    handleReset() {
      this.listQuery.goodsName = ''
      this.listQuery.page = 1
      this.getList()
    },
    handleDeletes() {
      if (this.chooseCommList.length == 0) {
        this.$message.error('请选择要删除的商品')
        return
      }
      this.handleDelete(this.chooseCommList.join(','))
    },
    beforeDel(row) {
      this.handleDelete(row.goodsId)
    },
    handleDelete(ids) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        return new Promise((resolve, reject) => {
          del({goodsIds: ids, activityId: this.id}).then(() => {
            loading.close()
            this.$message({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.chooseCommList = []
            this.getList()

          }).catch(error => {
            loading.close()
            reject(error)
          })
        })
      }).catch()
    },

    getList(obj) {
      this.listLoading = true
      this.commodityList = []
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        giveInfoId: this.id,
        goodsName: this.listQuery.goodsName
      }
      return new Promise((resolve, reject) => {
        queryPage(query).then(response => {
          this.listLoading = false
          this.commodityList = response.data.data.rows
          this.total = response.data.data.total
          console.log('list', this.commodityList)
        }).catch(error => {
          this.listLoading = false
          reject(error)
        })
      })
    },

    /**
     * 按商家添加，
     */
    chooseAll(shopInfo) {
      this.showDetail = false
      if (shopInfo.id) {
        this.$confirm('是否确定添加' + shopInfo.shopName + '所有商品为福利商品?', '提示', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning'
        }).then(() => {
          this.addGoodsByBusiness(shopInfo.id)
        }).catch(() => {

        })
      }

    },

    addGoodsByBusiness(shopId) {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      return new Promise((resolve, reject) => {
        addGoodsByBusiness({shopId: shopId, giveInfoId: this.id}).then((res) => {
          loading.close()
          this.$message({
            title: '成功',
            message: res.msg || '添加成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        }).catch(error => {
          loading.close()
          reject(error)
        })
      })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
@import "@/styles/mixin.scss";

$dark_gray: #bfced6;
.commodity-container {
  padding: 10px;

  .el-checkbox__inner {
    // border-color: #409eff;
    @include border-color(1px, solid, $mainColor);
    width: 20px;
    height: 20px;
  }

  .el-checkbox__inner::after {
    height: 10px;
    left: 7px;
  }

  .commodity-search {
    background-color: #fff;
    padding: 0 20px;

    .svg-container {
      padding: 0;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;

      &_login {
        font-size: 20px;
      }
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: "";
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .commodity-search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .commodity-search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }
  }

  .commodity-btn {
    display: flex;
    height: 36px;
    line-height: 36px;
    margin-bottom: 10px;
    margin-top: 10px;

    .btn-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      font-family: PingFangSC-Medium;
      color: rgba(51, 51, 51, 1);
    }
  }

  .commodity-list {
    min-height: 300px;

    .el-row {
      .el-col-5 {
        width: 20%;
      }
    }

    .commodity-list-item {
      background-color: #ffffff;
      padding: 10px 10px 0;
      margin-right: 10px;
      margin-bottom: 10px;
      box-shadow: 0 5px 10px rgba(254, 90, 97, 0.05);
      position: relative;

      .checkBox {
        position: absolute;
        left: 5px;
        top: 5px;
        // border: 1px solid #00a0ff;
        // z-index: 99999;
        // .kj {
        //   width: 25px;
        //   height: 25px;
        // }
      }

      .item-img {
        width: 100%;
        height: 183px;
        position: relative;
        text-align: center;

        .bg {
          width: 162px;
          height: 100%;
          z-index: 0;

          &:hover {
            transform: scale(1.1, 1.1);
            filter: contrast(130%);
          }
        }
      }

      .item-title {
        margin: 10px 0;
        font-family: "Arial-BoldMT", "Arial Bold", "Arial";
        font-weight: 600;
        font-style: normal;
        font-size: 15px;
        text-overflow: -o-ellipsis-lastline;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        -webkit-box-orient: vertical;
        height: 35px;
      }

      .item-mark {
        font-size: 12px;
        margin: 5px 0;
        line-height: 1.5;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        color: #333;

        .price {
          color: red;
          font-size: 16px;
        }
      }

      .item-type {
        height: 20px;
        line-height: 20px;

        .type-gate {
          margin-right: 5px;

          img {
            width: 40px;
          }
        }

        .type-exam {
          img {
            width: 40px;
          }
        }

        .type-num {
          color: #333333;
          font-weight: 400;
          font-size: 12px;
          float: right;
        }
      }

      .item-btns {
        display: flex;
        margin-top: 10px;
        position: relative;

        .el-button {
          flex: 1;

          span {
            font-size: 14px;
            font-weight: 500;
          }
        }

        &:before {
          content: "";
          position: absolute;
          top: 0;
          left: -10px;
          right: -10px;
          height: 1px;
          background-color: #e2e3e8;
        }
      }
    }
  }

  .pagination-container {
    background: none;
  }
}
</style>
